<?php

namespace App\Http\Middleware;

use App\Admin;
use Closure;

class CheckLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(!$request->session()->has('username'))
            return redirect('/login.html');

        $username = $request->session()->get('username');

        //账号是否有效
        if(
            !Admin::where('username',$username)->first() ||
            !Admin::where('username',$username)->first()['status']
        )
            return redirect('/login.html');

        return $next($request);
    }
}
